# coding:utf-8

import os

import set_env_path     # 请勿删除

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


Base = declarative_base()
engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
Session = sessionmaker(bind=engine)
session = Session()


def sync_outbound_user_id():
    """
    """
    select_outbound_sql = """
    select id, mobile from outbound_records
    """

    update_outbound_sql = """
        update outbound_records set user_id=:user_id, is_invite_success=FALSE where id=:id
    """

    select_user = """
    select id from user where mobile=:mobile
    """

    records = session.execute(select_outbound_sql).fetchall()

    for each in records:
        user = session.execute(select_user, {'mobile': each.mobile}).fetchone()
        if user:
            session.execute(
                update_outbound_sql, {'user_id': user.id, 'id': each.id}
            )
            session.commit()
        print(each.mobile)

if __name__ == '__main__':
    sync_outbound_user_id()
